package actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Select;

import model.TourRegister;

import com.opensymphony.xwork2.ActionSupport;

import dao.DataAccessObject;

public class TourRegisterAction extends ActionSupport {
	DataAccessObject dao = new DataAccessObject();
	private String name;
	private String tour;
	private Date time;
	private double price;
	
	
	@Override
	public void validate() {
		System.out.println("Validating...");
		if(name == null || name.trim().equals("")){
			addFieldError("name", "Name is required");
		}
		if(tour == null || tour.trim().equals("")){
			addFieldError("tour", "Tour is required");
		}
		
		String Date_PATTERN = "^((0[1-9])|([12][0-9])|(3[0-1]))(/)((0[1-9])|(1[0-2]))(/)\\d{4}$";
		Pattern pattern=Pattern.compile(Date_PATTERN);
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		if(time==null){
			addFieldError("time", "Please input time");
		}else{
			String dateFormat =sdf.format(time);
			Matcher matcher=pattern.matcher(dateFormat);
			if(!matcher.matches()){
				addFieldError("time", "Please input valid time [dd/mm/yyyy]");
			}
		}
		
		if(price <=0){
			addFieldError("price", "Price is required");
		}
		
		
	}
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		TourRegister tr = new TourRegister();
		tr.setName(this.name);
		tr.setTourName(this.tour);
		tr.setTime(this.time);
		tr.setPrice(this.price);
		dao.insertTourRegister(tr);
		if(name!=null && tour!=null && time!=null && price>0)
			addActionMessage("Your registration is sucessfull!");
		System.out.println("Register: "+name+" "+tour+" "+time+" "+price);
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTour() {
		return tour;
	}
	public void setTour(String tour) {
		this.tour = tour;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	
}
